package org.gdstash.db;

import org.gdstash.util.GDMsgFormatter;
import org.gdstash.util.GDMsgLogger;

import java.sql.*;
import java.util.LinkedList;
import java.util.List;

public class DBSkillSpawn {
    public static final String TABLE_NAME = "GD_SKILL_SPAWN";
    public static final String FIELD_ID = "SKILL_ID";
    private static final int ROW_SKILL_ID = 1;
    private static final int ROW_INDEX = 2;
    private static final int ROW_PET_ID = 3;
    private String skillID;
    private int index;
    private String petID;
    private DBPet dbPet;

    private DBSkillSpawn() {
    }

    public DBSkillSpawn(final int index, final String petID) {
        this.index = index;
        this.petID = petID;
    }

    public String getPetID() {
        return this.petID;
    }

    public DBPet getPet() {
        return this.dbPet;
    }

    public static void createTable(final Connection conn) throws SQLException {
        final String dropTable = "DROP TABLE GD_SKILL_SPAWN";
        final String createTable = "CREATE TABLE GD_SKILL_SPAWN (SKILL_ID  VARCHAR(256) NOT NULL, INDEX             INTEGER, PET_ID            VARCHAR(256), PRIMARY KEY (SKILL_ID, INDEX))";
        try (final Statement st = conn.createStatement()) {
            if (GDDBUtil.tableExists(conn, "GD_SKILL_SPAWN")) {
                st.execute(dropTable);
            }
            st.execute(createTable);
            st.close();
            conn.commit();
        } catch (SQLException ex) {
            final Object[] args = {"GD_SKILL_SPAWN"};
            final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_CREATE_TABLE", args);
            GDMsgLogger.addError(msg);
            throw ex;
        }
    }

    public static void insert(final Connection conn, final String skillID, final List<DBSkillSpawn> spawns) throws SQLException {
        final String insert = "INSERT INTO GD_SKILL_SPAWN VALUES (?,?,?)";
        if (spawns == null) {
            return;
        }
        if (spawns.isEmpty()) {
            return;
        }
        try (final PreparedStatement ps = conn.prepareStatement(insert)) {
            for (final DBSkillSpawn pet : spawns) {
                ps.setString(1, skillID);
                ps.setInt(2, pet.index);
                ps.setString(3, pet.petID);
                ps.executeUpdate();
                ps.clearParameters();
            }
            ps.close();
        }
    }

    public static List<DBSkillSpawn> getBySkillID(final String skillID) {
        List<DBSkillSpawn> list = new LinkedList<DBSkillSpawn>();
        final String command = "SELECT * FROM GD_SKILL_SPAWN WHERE SKILL_ID = ? ORDER BY INDEX";
        try (final Connection conn = GDDBData.getConnection();
             final PreparedStatement ps = conn.prepareStatement(command)) {
            ps.setString(1, skillID);
            try (final ResultSet rs = ps.executeQuery()) {
                list = wrap(rs);
                conn.commit();
            } catch (SQLException ex) {
                throw ex;
            }
        } catch (SQLException ex2) {
            final Object[] args = {skillID, "GD_SKILL_SPAWN"};
            final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_READ_TABLE_BY_ID", args);
            GDMsgLogger.addError(msg);
            GDMsgLogger.addError(ex2);
        }
        return list;
    }

    private static List<DBSkillSpawn> wrap(final ResultSet rs) throws SQLException {
        final LinkedList<DBSkillSpawn> list = new LinkedList<DBSkillSpawn>();
        while (rs.next()) {
            final DBSkillSpawn pet = new DBSkillSpawn();
            pet.skillID = rs.getString(1);
            pet.index = rs.getInt(2);
            pet.petID = rs.getString(3);
            if (pet.petID != null) {
                pet.dbPet = DBPet.get(pet.petID);
            }
            list.add(pet);
        }
        return list;
    }
}
